package server;

import java.io.IOException;
import java.net.Socket;

public class ServerThread implements Runnable{
	private Socket socket;
	private Socket socketTransfert;
	private SimpleFTPServer simpleFTPServer;
	
	public ServerThread(Socket s){
		socket = s;
		simpleFTPServer = new SimpleFTPServer(s);
	}
	
    public void finalize(){
    	try {
			socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void run(){
		String commande;
		System.out.println("Une nouvelle connection a été établie");
		simpleFTPServer.connect();
		while(true){
			try {
				commande = simpleFTPServer.readLine();
				if(commande.equals("QUIT"))
					break;
				else if(commande.equals("PWD"))
					simpleFTPServer.pwd();
				else if(commande.equals("PASV")) {
					simpleFTPServer.passv();
					commande = simpleFTPServer.readLine();
					if(commande.startsWith("STOR"))
						simpleFTPServer.stor(commande.substring(5));
					else if(commande.equals("LIST"))
						simpleFTPServer.list();
					else
						break;
				}
				else
					break;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
